package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.Goods;
import com.dingreading.cloud.admin.entity.table.GoodsTableDef;
import com.dingreading.cloud.admin.mapper.GoodsMapper;
import com.dingreading.cloud.admin.service.GoodsService;
import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.dingreading.cloud.admin.entity.table.GoodsCategoryLogTableDef.goodsCategoryLog;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class GoodsServiceImpl extends BaseServiceImpl<GoodsMapper, Goods, GoodsTableDef> implements GoodsService {

    @Override
    protected GoodsTableDef getTable() {
        return GoodsTableDef.goods;
    }

    @Override
    public Page<Goods> pageListV3(PageUtil pageUtil, String name, Long categoryId, Integer enabled, Integer realStatus) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(name))
            condition.and(table.name.like(name));
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));
        if (realStatus != null)
            condition.and(table.realStatus.eq(realStatus));

        QueryWrapper wrapper = null;
        if (categoryId != null) {
            condition.and(goodsCategoryLog.categoryId.eq(categoryId));

            wrapper = QueryWrapper.create()
                    .select()
                    .from(table)
                    .rightJoin(goodsCategoryLog).on(table.id.eq(goodsCategoryLog.goodsId))
                    .where(condition)
                    .orderBy(table.enabled.desc(), table.hotStatus.desc(), goodsCategoryLog.orderNum.asc());
        } else {
            wrapper = QueryWrapper.create()
                    .select()
                    .from(table)
                    .where(condition)
                    .orderBy(table.enabled.desc(), table.hotStatus.desc(), table.id.desc());
        }
        return getMapper().paginateWithRelations(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public boolean updateEnabledV3(Long id, Integer enabled, String staffUid, String staffName) {
        return UpdateChain.of(Goods.class)
                .set(table.enabled, enabled)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public boolean haveName(Long id, String name) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.name.eq(name));
        if (id != null)
            condition.and(table.id.ne(id));

        return exists(condition);
    }

    @Override
    public long dropBean(Long id) {
        return getMapper().deleteByCondition(table.id.eq(id));
    }

    @Override
    public void updateLastStaffInfo(Long id, String staffUid, String staffName) {
        UpdateChain.of(Goods.class)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public List<Goods> listByCategoryIdOrIdIn(String categoryId, List<Long> ids) {
        QueryCondition condition = table.categoryIds.like(",7,")
                .or(table.id.in(ids));
        return getMapper().selectListByCondition(condition);
    }

    @Override
    public boolean haveFreeDeliveryId(Long freeDeliveryId) {
        return exists(table.freeDeliveryId.eq(freeDeliveryId));
    }

    @Override
    public Goods getDataById(Long id) {
        return getMapper().selectOneWithRelationsByCondition(table.id.eq(id));
    }

    @Override
    public PageDto<Goods> pageByCoupon(PageUtil pageUtil, String keyword, Long categoryId) {
        QueryCondition condition = table.couponStatus.eq(1)
                .and(table.enabled.eq(1));
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.name.like(keyword));

        QueryWrapper wrapper = null;
        if (categoryId != null) {
            condition.and(goodsCategoryLog.categoryId.eq(categoryId));

            wrapper = QueryWrapper.create()
                    .select()
                    .from(table)
                    .rightJoin(goodsCategoryLog).on(table.id.eq(goodsCategoryLog.goodsId))
                    .where(condition)
                    .orderBy(table.id.desc(),goodsCategoryLog.orderNum.asc());
        } else {
            wrapper = QueryWrapper.create()
                    .select()
                    .from(table)
                    .where(condition)
                    .orderBy(table.id.desc());
        }

        Page<Goods> page = getMapper().paginateWithRelations(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
        PageDto<Goods> data = new PageDto<>();
        data.setTotal(page.getTotalRow());
        data.setList(page.getRecords());
        return data;
    }

}
